<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="标签" align="center" prop="tag" />
      <el-table-column label="简介" align="center" prop="description" />
      <el-table-column label="数据" align="center" prop="data" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['home:data:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改数据页对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="dataRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入标签" disabled />
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="form.description" placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="数据" prop="data">
          <template v-if="form.id == 10 || form.id == 11">
            <div>附码总数</div>
            <el-input v-model="firstData.value1" placeholder="请输入附码总数" />
            <div>用码总数</div>
            <el-input v-model="firstData.value2" placeholder="请输入附码总数" />
            <div>绿码数</div>
            <el-input v-model="firstData.value3" placeholder="请输入附码总数" />
            <div>黄码数</div>
            <el-input v-model="firstData.value4" placeholder="请输入附码总数" />
            <div>红码数</div>
            <el-input v-model="firstData.value5" placeholder="请输入附码总数" />
          </template>
          <template v-if="form.id == 15">
            <div>绿码数据</div>
            <div v-for="(item, index) in greenData" :key="index" class="flex">
              <el-input v-model="item.name" placeholder="请输入主体名称" />
              <el-input v-model="item.tel" placeholder="请输入联系电话" />
              <el-input v-model="item.linkman" placeholder="请输入联系人" />
              <el-input v-model="item.address" placeholder="请输入所属区划" />
              <el-input
                v-model="item.description"
                placeholder="请输入转码说明"
              />
            </div>
            <el-button type="primary" @click="addGreenData()">添加</el-button>
            <el-button
              type="danger"
              @click="deleteGreenData()"
              :disabled="greenData.length == 1"
              >删除</el-button
            >
          </template>
          <template v-if="form.id == 16">
            <div>黄码数据</div>
            <div v-for="(item, index) in yellowData" :key="index" class="flex">
              <el-input v-model="item.name" placeholder="请输入主体名称" />
              <el-input v-model="item.tel" placeholder="请输入联系电话" />
              <el-input v-model="item.linkman" placeholder="请输入联系人" />
              <el-input v-model="item.address" placeholder="请输入所属区划" />
              <el-input
                v-model="item.description"
                placeholder="请输入转码说明"
              />
            </div>
            <el-button type="primary" @click="addYellowData()">添加</el-button>
            <el-button
              type="danger"
              @click="deleteYellowData()"
              :disabled="yellowData.length == 1"
              >删除</el-button
            >
          </template>
          <template v-if="form.id == 17">
            <div>红码数据</div>
            <div v-for="(item, index) in redData" :key="index" class="flex">
              <el-input v-model="item.name" placeholder="请输入主体名称" />
              <el-input v-model="item.tel" placeholder="请输入联系电话" />
              <el-input v-model="item.linkman" placeholder="请输入联系人" />
              <el-input v-model="item.address" placeholder="请输入所属区划" />
              <el-input
                v-model="item.description"
                placeholder="请输入转码说明"
              />
            </div>
            <el-button type="primary" @click="addRedData()">添加</el-button>
            <el-button
              type="danger"
              @click="deleteRedData()"
              :disabled="redData.length == 1"
              >删除</el-button
            >
          </template>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Data">
import {
  listData,
  getData,
  delData,
  addData,
  updateData,
} from '@/api/home/data'

const { proxy } = getCurrentInstance()

const dataList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    positionId: 4,
    tag: null,
    data: null,
    description: null,
  },
  rules: {
    positionId: [
      { required: true, message: '位置ID不能为空', trigger: 'blur' },
    ],
  },
  firstData: {
    value1: '',
    value2: '',
    value3: '',
    value4: '',
    value5: '',
  },
  greenData: [
    {
      name: '',
      tel: '',
      linkman: '',
      address: '',
      description: '',
    },
  ],
  yellowData: [
    {
      name: '',
      tel: '',
      linkman: '',
      address: '',
      description: '',
    },
  ],
  redData: [
    {
      name: '',
      tel: '',
      linkman: '',
      address: '',
      description: '',
    },
  ],
})

const { queryParams, form, rules, firstData, greenData, yellowData, redData } =
  toRefs(data)

/** 查询数据页列表 */
function getList() {
  loading.value = true
  listData(queryParams.value).then((response) => {
    dataList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    positionId: 0,
    tag: null,
    data: null,
    description: null,
  }
  proxy.resetForm('dataRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加数据页'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getData(_id).then((response) => {
    form.value = response.data
    if (form.value.id == 10 || form.value.id == 11) {
      data.firstData = {
        value1: '',
        value2: '',
        value3: '',
        value4: '',
        value5: '',
      }
    }
    if (form.value.id == 15) {
      data.greenData = JSON.parse(form.value.data)
    }
    if (form.value.id == 16) {
      data.yellowData = JSON.parse(form.value.data)
    }
    if (form.value.id == 17) {
      data.redData = JSON.parse(form.value.data)
    }
    open.value = true
    title.value = '修改数据页'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['dataRef'].validate((valid) => {
    if (valid) {
      if (form.value.id == 10 || form.value.id == 11) {
        form.value.data = JSON.stringify(data.firstData)
      }
      if (form.value.id == 15) {
        form.value.data = JSON.stringify(data.greenData)
      }
      if (form.value.id == 16) {
        form.value.data = JSON.stringify(data.yellowData)
      }
      if (form.value.id == 17) {
        form.value.data = JSON.stringify(data.redData)
      }
      if (form.value.id != null) {
        updateData(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addData(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除数据页编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delData(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'home/data/export',
    {
      ...queryParams.value,
    },
    `data_${new Date().getTime()}.xlsx`
  )
}

function addGreenData() {
  greenData.value.push({
    name: '',
    tel: '',
    linkman: '',
    address: '',
    description: '',
  })
}

function deleteGreenData() {
  greenData.value.pop()
}

function addYellowData() {
  yellowData.value.push({
    name: '',
    tel: '',
    linkman: '',
    address: '',
    description: '',
  })
}

function deleteYellowData() {
  yellowData.value.pop()
}

function addRedData() {
  redData.value.push({
    name: '',
    tel: '',
    linkman: '',
    address: '',
    description: '',
  })
}

function deleteRedData() {
  redData.value.pop()
}

getList()
</script>
